/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ses;

import Entity.Accounts;
import ses.exceptions.IllegalOrphanException;
import ses.exceptions.NonexistentEntityException;
import ses.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entity.AccountsType;
import Entity.Advertise;
import Entity.Driver;
import Entity.AccountDetails;
import Entity.Company;
import Entity.Order1;
import java.util.ArrayList;
import java.util.List;
import Entity.Feedback;
import Entity.PaymentDetails;
import javax.transaction.UserTransaction;

/**
 *
 * @author Huu Vinh
 */
public class AccountsJpaController implements Serializable {

    public AccountsJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Accounts accounts) throws RollbackFailureException, Exception {
        if (accounts.getOrder1List() == null) {
            accounts.setOrder1List(new ArrayList<Order1>());
        }
        if (accounts.getFeedbackList() == null) {
            accounts.setFeedbackList(new ArrayList<Feedback>());
        }
        if (accounts.getPaymentDetailsList() == null) {
            accounts.setPaymentDetailsList(new ArrayList<PaymentDetails>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            AccountsType typeId = accounts.getTypeId();
            if (typeId != null) {
                typeId = em.getReference(typeId.getClass(), typeId.getTypeId());
                accounts.setTypeId(typeId);
            }
            Advertise advertise = accounts.getAdvertise();
            if (advertise != null) {
                advertise = em.getReference(advertise.getClass(), advertise.getAdvertiseId());
                accounts.setAdvertise(advertise);
            }
            Driver driver = accounts.getDriver();
            if (driver != null) {
                driver = em.getReference(driver.getClass(), driver.getDriverId());
                accounts.setDriver(driver);
            }
            AccountDetails accountDetails = accounts.getAccountDetails();
            if (accountDetails != null) {
                accountDetails = em.getReference(accountDetails.getClass(), accountDetails.getAccId());
                accounts.setAccountDetails(accountDetails);
            }
            Company company = accounts.getCompany();
            if (company != null) {
                company = em.getReference(company.getClass(), company.getCompanyId());
                accounts.setCompany(company);
            }
            List<Order1> attachedOrder1List = new ArrayList<Order1>();
            for (Order1 order1ListOrder1ToAttach : accounts.getOrder1List()) {
                order1ListOrder1ToAttach = em.getReference(order1ListOrder1ToAttach.getClass(), order1ListOrder1ToAttach.getOrderId());
                attachedOrder1List.add(order1ListOrder1ToAttach);
            }
            accounts.setOrder1List(attachedOrder1List);
            List<Feedback> attachedFeedbackList = new ArrayList<Feedback>();
            for (Feedback feedbackListFeedbackToAttach : accounts.getFeedbackList()) {
                feedbackListFeedbackToAttach = em.getReference(feedbackListFeedbackToAttach.getClass(), feedbackListFeedbackToAttach.getFBId());
                attachedFeedbackList.add(feedbackListFeedbackToAttach);
            }
            accounts.setFeedbackList(attachedFeedbackList);
            List<PaymentDetails> attachedPaymentDetailsList = new ArrayList<PaymentDetails>();
            for (PaymentDetails paymentDetailsListPaymentDetailsToAttach : accounts.getPaymentDetailsList()) {
                paymentDetailsListPaymentDetailsToAttach = em.getReference(paymentDetailsListPaymentDetailsToAttach.getClass(), paymentDetailsListPaymentDetailsToAttach.getPaymentDetailsPK());
                attachedPaymentDetailsList.add(paymentDetailsListPaymentDetailsToAttach);
            }
            accounts.setPaymentDetailsList(attachedPaymentDetailsList);
            em.persist(accounts);
            if (typeId != null) {
                typeId.getAccountsList().add(accounts);
                typeId = em.merge(typeId);
            }
            if (advertise != null) {
                Accounts oldAccountsOfAdvertise = advertise.getAccounts();
                if (oldAccountsOfAdvertise != null) {
                    oldAccountsOfAdvertise.setAdvertise(null);
                    oldAccountsOfAdvertise = em.merge(oldAccountsOfAdvertise);
                }
                advertise.setAccounts(accounts);
                advertise = em.merge(advertise);
            }
            if (driver != null) {
                Accounts oldAccountsOfDriver = driver.getAccounts();
                if (oldAccountsOfDriver != null) {
                    oldAccountsOfDriver.setDriver(null);
                    oldAccountsOfDriver = em.merge(oldAccountsOfDriver);
                }
                driver.setAccounts(accounts);
                driver = em.merge(driver);
            }
            if (accountDetails != null) {
                Accounts oldAccountsOfAccountDetails = accountDetails.getAccounts();
                if (oldAccountsOfAccountDetails != null) {
                    oldAccountsOfAccountDetails.setAccountDetails(null);
                    oldAccountsOfAccountDetails = em.merge(oldAccountsOfAccountDetails);
                }
                accountDetails.setAccounts(accounts);
                accountDetails = em.merge(accountDetails);
            }
            if (company != null) {
                Accounts oldAccountsOfCompany = company.getAccounts();
                if (oldAccountsOfCompany != null) {
                    oldAccountsOfCompany.setCompany(null);
                    oldAccountsOfCompany = em.merge(oldAccountsOfCompany);
                }
                company.setAccounts(accounts);
                company = em.merge(company);
            }
            for (Order1 order1ListOrder1 : accounts.getOrder1List()) {
                Accounts oldAccIdOfOrder1ListOrder1 = order1ListOrder1.getAccId();
                order1ListOrder1.setAccId(accounts);
                order1ListOrder1 = em.merge(order1ListOrder1);
                if (oldAccIdOfOrder1ListOrder1 != null) {
                    oldAccIdOfOrder1ListOrder1.getOrder1List().remove(order1ListOrder1);
                    oldAccIdOfOrder1ListOrder1 = em.merge(oldAccIdOfOrder1ListOrder1);
                }
            }
            for (Feedback feedbackListFeedback : accounts.getFeedbackList()) {
                Accounts oldAccountIdOfFeedbackListFeedback = feedbackListFeedback.getAccountId();
                feedbackListFeedback.setAccountId(accounts);
                feedbackListFeedback = em.merge(feedbackListFeedback);
                if (oldAccountIdOfFeedbackListFeedback != null) {
                    oldAccountIdOfFeedbackListFeedback.getFeedbackList().remove(feedbackListFeedback);
                    oldAccountIdOfFeedbackListFeedback = em.merge(oldAccountIdOfFeedbackListFeedback);
                }
            }
            for (PaymentDetails paymentDetailsListPaymentDetails : accounts.getPaymentDetailsList()) {
                Accounts oldAccountsOfPaymentDetailsListPaymentDetails = paymentDetailsListPaymentDetails.getAccounts();
                paymentDetailsListPaymentDetails.setAccounts(accounts);
                paymentDetailsListPaymentDetails = em.merge(paymentDetailsListPaymentDetails);
                if (oldAccountsOfPaymentDetailsListPaymentDetails != null) {
                    oldAccountsOfPaymentDetailsListPaymentDetails.getPaymentDetailsList().remove(paymentDetailsListPaymentDetails);
                    oldAccountsOfPaymentDetailsListPaymentDetails = em.merge(oldAccountsOfPaymentDetailsListPaymentDetails);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Accounts accounts) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Accounts persistentAccounts = em.find(Accounts.class, accounts.getAccId());
            AccountsType typeIdOld = persistentAccounts.getTypeId();
            AccountsType typeIdNew = accounts.getTypeId();
            Advertise advertiseOld = persistentAccounts.getAdvertise();
            Advertise advertiseNew = accounts.getAdvertise();
            Driver driverOld = persistentAccounts.getDriver();
            Driver driverNew = accounts.getDriver();
            AccountDetails accountDetailsOld = persistentAccounts.getAccountDetails();
            AccountDetails accountDetailsNew = accounts.getAccountDetails();
            Company companyOld = persistentAccounts.getCompany();
            Company companyNew = accounts.getCompany();
            List<Order1> order1ListOld = persistentAccounts.getOrder1List();
            List<Order1> order1ListNew = accounts.getOrder1List();
            List<Feedback> feedbackListOld = persistentAccounts.getFeedbackList();
            List<Feedback> feedbackListNew = accounts.getFeedbackList();
            List<PaymentDetails> paymentDetailsListOld = persistentAccounts.getPaymentDetailsList();
            List<PaymentDetails> paymentDetailsListNew = accounts.getPaymentDetailsList();
            List<String> illegalOrphanMessages = null;
            if (advertiseOld != null && !advertiseOld.equals(advertiseNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Advertise " + advertiseOld + " since its accounts field is not nullable.");
            }
            if (driverOld != null && !driverOld.equals(driverNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Driver " + driverOld + " since its accounts field is not nullable.");
            }
            if (accountDetailsOld != null && !accountDetailsOld.equals(accountDetailsNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain AccountDetails " + accountDetailsOld + " since its accounts field is not nullable.");
            }
            if (companyOld != null && !companyOld.equals(companyNew)) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("You must retain Company " + companyOld + " since its accounts field is not nullable.");
            }
            for (Order1 order1ListOldOrder1 : order1ListOld) {
                if (!order1ListNew.contains(order1ListOldOrder1)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Order1 " + order1ListOldOrder1 + " since its accId field is not nullable.");
                }
            }
            for (PaymentDetails paymentDetailsListOldPaymentDetails : paymentDetailsListOld) {
                if (!paymentDetailsListNew.contains(paymentDetailsListOldPaymentDetails)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain PaymentDetails " + paymentDetailsListOldPaymentDetails + " since its accounts field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (typeIdNew != null) {
                typeIdNew = em.getReference(typeIdNew.getClass(), typeIdNew.getTypeId());
                accounts.setTypeId(typeIdNew);
            }
            if (advertiseNew != null) {
                advertiseNew = em.getReference(advertiseNew.getClass(), advertiseNew.getAdvertiseId());
                accounts.setAdvertise(advertiseNew);
            }
            if (driverNew != null) {
                driverNew = em.getReference(driverNew.getClass(), driverNew.getDriverId());
                accounts.setDriver(driverNew);
            }
            if (accountDetailsNew != null) {
                accountDetailsNew = em.getReference(accountDetailsNew.getClass(), accountDetailsNew.getAccId());
                accounts.setAccountDetails(accountDetailsNew);
            }
            if (companyNew != null) {
                companyNew = em.getReference(companyNew.getClass(), companyNew.getCompanyId());
                accounts.setCompany(companyNew);
            }
            List<Order1> attachedOrder1ListNew = new ArrayList<Order1>();
            for (Order1 order1ListNewOrder1ToAttach : order1ListNew) {
                order1ListNewOrder1ToAttach = em.getReference(order1ListNewOrder1ToAttach.getClass(), order1ListNewOrder1ToAttach.getOrderId());
                attachedOrder1ListNew.add(order1ListNewOrder1ToAttach);
            }
            order1ListNew = attachedOrder1ListNew;
            accounts.setOrder1List(order1ListNew);
            List<Feedback> attachedFeedbackListNew = new ArrayList<Feedback>();
            for (Feedback feedbackListNewFeedbackToAttach : feedbackListNew) {
                feedbackListNewFeedbackToAttach = em.getReference(feedbackListNewFeedbackToAttach.getClass(), feedbackListNewFeedbackToAttach.getFBId());
                attachedFeedbackListNew.add(feedbackListNewFeedbackToAttach);
            }
            feedbackListNew = attachedFeedbackListNew;
            accounts.setFeedbackList(feedbackListNew);
            List<PaymentDetails> attachedPaymentDetailsListNew = new ArrayList<PaymentDetails>();
            for (PaymentDetails paymentDetailsListNewPaymentDetailsToAttach : paymentDetailsListNew) {
                paymentDetailsListNewPaymentDetailsToAttach = em.getReference(paymentDetailsListNewPaymentDetailsToAttach.getClass(), paymentDetailsListNewPaymentDetailsToAttach.getPaymentDetailsPK());
                attachedPaymentDetailsListNew.add(paymentDetailsListNewPaymentDetailsToAttach);
            }
            paymentDetailsListNew = attachedPaymentDetailsListNew;
            accounts.setPaymentDetailsList(paymentDetailsListNew);
            accounts = em.merge(accounts);
            if (typeIdOld != null && !typeIdOld.equals(typeIdNew)) {
                typeIdOld.getAccountsList().remove(accounts);
                typeIdOld = em.merge(typeIdOld);
            }
            if (typeIdNew != null && !typeIdNew.equals(typeIdOld)) {
                typeIdNew.getAccountsList().add(accounts);
                typeIdNew = em.merge(typeIdNew);
            }
            if (advertiseNew != null && !advertiseNew.equals(advertiseOld)) {
                Accounts oldAccountsOfAdvertise = advertiseNew.getAccounts();
                if (oldAccountsOfAdvertise != null) {
                    oldAccountsOfAdvertise.setAdvertise(null);
                    oldAccountsOfAdvertise = em.merge(oldAccountsOfAdvertise);
                }
                advertiseNew.setAccounts(accounts);
                advertiseNew = em.merge(advertiseNew);
            }
            if (driverNew != null && !driverNew.equals(driverOld)) {
                Accounts oldAccountsOfDriver = driverNew.getAccounts();
                if (oldAccountsOfDriver != null) {
                    oldAccountsOfDriver.setDriver(null);
                    oldAccountsOfDriver = em.merge(oldAccountsOfDriver);
                }
                driverNew.setAccounts(accounts);
                driverNew = em.merge(driverNew);
            }
            if (accountDetailsNew != null && !accountDetailsNew.equals(accountDetailsOld)) {
                Accounts oldAccountsOfAccountDetails = accountDetailsNew.getAccounts();
                if (oldAccountsOfAccountDetails != null) {
                    oldAccountsOfAccountDetails.setAccountDetails(null);
                    oldAccountsOfAccountDetails = em.merge(oldAccountsOfAccountDetails);
                }
                accountDetailsNew.setAccounts(accounts);
                accountDetailsNew = em.merge(accountDetailsNew);
            }
            if (companyNew != null && !companyNew.equals(companyOld)) {
                Accounts oldAccountsOfCompany = companyNew.getAccounts();
                if (oldAccountsOfCompany != null) {
                    oldAccountsOfCompany.setCompany(null);
                    oldAccountsOfCompany = em.merge(oldAccountsOfCompany);
                }
                companyNew.setAccounts(accounts);
                companyNew = em.merge(companyNew);
            }
            for (Order1 order1ListNewOrder1 : order1ListNew) {
                if (!order1ListOld.contains(order1ListNewOrder1)) {
                    Accounts oldAccIdOfOrder1ListNewOrder1 = order1ListNewOrder1.getAccId();
                    order1ListNewOrder1.setAccId(accounts);
                    order1ListNewOrder1 = em.merge(order1ListNewOrder1);
                    if (oldAccIdOfOrder1ListNewOrder1 != null && !oldAccIdOfOrder1ListNewOrder1.equals(accounts)) {
                        oldAccIdOfOrder1ListNewOrder1.getOrder1List().remove(order1ListNewOrder1);
                        oldAccIdOfOrder1ListNewOrder1 = em.merge(oldAccIdOfOrder1ListNewOrder1);
                    }
                }
            }
            for (Feedback feedbackListOldFeedback : feedbackListOld) {
                if (!feedbackListNew.contains(feedbackListOldFeedback)) {
                    feedbackListOldFeedback.setAccountId(null);
                    feedbackListOldFeedback = em.merge(feedbackListOldFeedback);
                }
            }
            for (Feedback feedbackListNewFeedback : feedbackListNew) {
                if (!feedbackListOld.contains(feedbackListNewFeedback)) {
                    Accounts oldAccountIdOfFeedbackListNewFeedback = feedbackListNewFeedback.getAccountId();
                    feedbackListNewFeedback.setAccountId(accounts);
                    feedbackListNewFeedback = em.merge(feedbackListNewFeedback);
                    if (oldAccountIdOfFeedbackListNewFeedback != null && !oldAccountIdOfFeedbackListNewFeedback.equals(accounts)) {
                        oldAccountIdOfFeedbackListNewFeedback.getFeedbackList().remove(feedbackListNewFeedback);
                        oldAccountIdOfFeedbackListNewFeedback = em.merge(oldAccountIdOfFeedbackListNewFeedback);
                    }
                }
            }
            for (PaymentDetails paymentDetailsListNewPaymentDetails : paymentDetailsListNew) {
                if (!paymentDetailsListOld.contains(paymentDetailsListNewPaymentDetails)) {
                    Accounts oldAccountsOfPaymentDetailsListNewPaymentDetails = paymentDetailsListNewPaymentDetails.getAccounts();
                    paymentDetailsListNewPaymentDetails.setAccounts(accounts);
                    paymentDetailsListNewPaymentDetails = em.merge(paymentDetailsListNewPaymentDetails);
                    if (oldAccountsOfPaymentDetailsListNewPaymentDetails != null && !oldAccountsOfPaymentDetailsListNewPaymentDetails.equals(accounts)) {
                        oldAccountsOfPaymentDetailsListNewPaymentDetails.getPaymentDetailsList().remove(paymentDetailsListNewPaymentDetails);
                        oldAccountsOfPaymentDetailsListNewPaymentDetails = em.merge(oldAccountsOfPaymentDetailsListNewPaymentDetails);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = accounts.getAccId();
                if (findAccounts(id) == null) {
                    throw new NonexistentEntityException("The accounts with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Accounts accounts;
            try {
                accounts = em.getReference(Accounts.class, id);
                accounts.getAccId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The accounts with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Advertise advertiseOrphanCheck = accounts.getAdvertise();
            if (advertiseOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Accounts (" + accounts + ") cannot be destroyed since the Advertise " + advertiseOrphanCheck + " in its advertise field has a non-nullable accounts field.");
            }
            Driver driverOrphanCheck = accounts.getDriver();
            if (driverOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Accounts (" + accounts + ") cannot be destroyed since the Driver " + driverOrphanCheck + " in its driver field has a non-nullable accounts field.");
            }
            AccountDetails accountDetailsOrphanCheck = accounts.getAccountDetails();
            if (accountDetailsOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Accounts (" + accounts + ") cannot be destroyed since the AccountDetails " + accountDetailsOrphanCheck + " in its accountDetails field has a non-nullable accounts field.");
            }
            Company companyOrphanCheck = accounts.getCompany();
            if (companyOrphanCheck != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Accounts (" + accounts + ") cannot be destroyed since the Company " + companyOrphanCheck + " in its company field has a non-nullable accounts field.");
            }
            List<Order1> order1ListOrphanCheck = accounts.getOrder1List();
            for (Order1 order1ListOrphanCheckOrder1 : order1ListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Accounts (" + accounts + ") cannot be destroyed since the Order1 " + order1ListOrphanCheckOrder1 + " in its order1List field has a non-nullable accId field.");
            }
            List<PaymentDetails> paymentDetailsListOrphanCheck = accounts.getPaymentDetailsList();
            for (PaymentDetails paymentDetailsListOrphanCheckPaymentDetails : paymentDetailsListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Accounts (" + accounts + ") cannot be destroyed since the PaymentDetails " + paymentDetailsListOrphanCheckPaymentDetails + " in its paymentDetailsList field has a non-nullable accounts field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            AccountsType typeId = accounts.getTypeId();
            if (typeId != null) {
                typeId.getAccountsList().remove(accounts);
                typeId = em.merge(typeId);
            }
            List<Feedback> feedbackList = accounts.getFeedbackList();
            for (Feedback feedbackListFeedback : feedbackList) {
                feedbackListFeedback.setAccountId(null);
                feedbackListFeedback = em.merge(feedbackListFeedback);
            }
            em.remove(accounts);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Accounts> findAccountsEntities() {
        return findAccountsEntities(true, -1, -1);
    }

    public List<Accounts> findAccountsEntities(int maxResults, int firstResult) {
        return findAccountsEntities(false, maxResults, firstResult);
    }

    private List<Accounts> findAccountsEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Accounts as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Accounts findAccounts(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Accounts.class, id);
        } finally {
            em.close();
        }
    }

    public int getAccountsCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Accounts as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
